// source-selection.js - 词库和分组选择组件
const app = getApp();

Component({
  data: {
    books: [],
    groups: [],
    dictationModes: [
      { id: 'en_to_zh', name: '听英文写中文' },
      { id: 'zh_to_en', name: '听中文写英文' },
      { id: 'random', name: '随机' }
    ],
    selectedBookId: '',
    selectedGroup: '',
    selectedMode: 'en_to_zh',
    
    // 答题模式
    answeringModes: [
      { id: 'online', name: '线上答题' },
      { id: 'offline', name: '线下答题' }
    ],
    selectedAnsweringMode: 'online',
    
    // 作答时间设置
    timeLimitEnabled: false,
    timeLimit: 30,
    timeLimitOptions: [10, 20, 30, 40, 50, 60]
  },
  lifetimes: {
    attached() {
      // 获取DictationManager实例
      this.dictationManager = app.getDictationManager();
      
      // 添加状态变化监听器
      this.stateChangeListener = this.onDictationStateChange.bind(this);
      this.dictationManager.addListener(this.stateChangeListener);
      
      // 初始化组件数据
      this.updateComponentData();
    },
    detached() {
      // 移除状态变化监听器
      if (this.stateChangeListener) {
        this.dictationManager.removeListener(this.stateChangeListener);
      }
    }
  },
  methods: {
    /**
     * 监听DictationManager状态变化
     */
    onDictationStateChange(state) {
      this.setData({
        books: state.books,
        groups: state.groups,
        selectedBookId: state.selectedBookId,
        selectedGroup: state.selectedGroup,
        selectedMode: state.selectedMode,
        selectedAnsweringMode: state.answeringMode,
        timeLimitEnabled: state.timeLimitEnabled,
        timeLimit: state.timeLimit
      });
    },
    
    /**
     * 更新组件数据
     */
    updateComponentData() {
      const state = this.dictationManager.getState();
      this.setData({
        books: state.books,
        groups: state.groups,
        selectedBookId: state.selectedBookId,
        selectedGroup: state.selectedGroup,
        selectedMode: state.selectedMode,
        selectedAnsweringMode: state.answeringMode,
        timeLimitEnabled: state.timeLimitEnabled,
        timeLimit: state.timeLimit
      });
    },
    
    /**
     * 选择答题模式
     */
    selectAnsweringMode(e) {
      const mode = e.currentTarget.dataset.mode;
      
      // 更新状态
      this.dictationManager.setAnsweringMode(mode);
      this.setData({
        selectedAnsweringMode: mode
      });
    },
    
    /**
     * 切换时间限制开关
     */
    toggleTimeLimit(e) {
      const enabled = e.detail.value;
      const { timeLimit } = this.data;
      
      // 更新状态
      this.dictationManager.setTimeLimit(enabled, timeLimit);
      this.setData({
        timeLimitEnabled: enabled
      });
    },
    
    /**
     * 选择作答时间
     */
    selectTimeLimit(e) {
      const limit = parseInt(e.currentTarget.dataset.limit);
      const { timeLimitEnabled } = this.data;
      
      // 更新状态
      this.dictationManager.setTimeLimit(timeLimitEnabled, limit);
      this.setData({
        timeLimit: limit
      });
    },

    /**
     * 选择词库
     */
    selectBook(e) {
      const bookId = e.currentTarget.dataset.id;
      
      // 立即在组件本地重置分组数据，避免显示不属于当前词库的分组
      this.setData({
        selectedBookId: bookId,
        selectedGroup: '',
        groups: []
      });
      
      // 更新管理器状态
      this.dictationManager.setState({
        selectedBookId: bookId,
        selectedGroup: ''
      });
      
      // 加载分组
      this.dictationManager.loadGroupsFromSelectedBook(bookId).then(() => {
        // 加载分组后更新组件数据
        this.updateComponentData();
      });
    },

    /**
     * 选择分组
     */
    selectGroup(e) {
      const group = e.currentTarget.dataset.group;
      
      // 更新状态
      this.dictationManager.setState({
        selectedGroup: group
      });
    },

    /**
     * 选择听写模式
     */
    selectMode(e) {
      const mode = e.currentTarget.dataset.mode;
      
      // 更新状态
      this.dictationManager.setState({
        selectedMode: mode
      });
    },

    /**
     * 开始听写
     */
    startDictation() {
      const { selectedBookId, selectedGroup, selectedMode } = this.data;
      
      if (!selectedBookId) {
        wx.showToast({
          title: '请选择词库',
          icon: 'none'
        });
        return;
      }
      
      // 如果选择了分组，需要添加词书id前缀
      const groupWithPrefix = selectedGroup ? `${selectedBookId}-${selectedGroup}` : '';
      
      // 直接调用管理器的方法开始听写
      this.dictationManager.startDictation(selectedBookId, groupWithPrefix, selectedMode);
    }
  }
});